{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多个数据元组\n",
    "t1 = (10, 20, 30)\n",
    "\n",
    "# 单个数据元组\n",
    "t2 = (10,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "<class 'int'>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "t2 = (10,)\n",
    "print(type(t2))  # tuple\n",
    "\n",
    "t3 = (20)\n",
    "print(type(t3))  # int\n",
    "\n",
    "t4 = ('hello')\n",
    "print(type(t4))  # str"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组的常见操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aa\n"
     ]
    }
   ],
   "source": [
    "tuple1 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(tuple1[0])  # aa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "tuple1 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(tuple1.index('aa'))  # 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "tuple1 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(tuple1.count('bb'))  # 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "tuple1 = ('aa', 'bb', 'cc', 'bb')\n",
    "print(len(tuple1))  # 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-aaa8d10a8057>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mtuple1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'aa'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'bb'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'cc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'bb'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtuple1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'aaa'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "tuple1 = ('aa', 'bb', 'cc', 'bb')\n",
    "tuple1[0] = 'aaa'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aa', 'bb', 'cc']\n",
      "(10, 20, ['aaaaa', 'bb', 'cc'], 50, 30)\n"
     ]
    }
   ],
   "source": [
    "tuple2 = (10, 20, ['aa', 'bb', 'cc'], 50, 30)\n",
    "print(tuple2[2])  # 访问到列表\n",
    "\n",
    "# 结果：(10, 20, ['aaaaa', 'bb', 'cc'], 50, 30)\n",
    "tuple2[2][0] = 'aaaaa'\n",
    "print(tuple2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
